<?php

namespace App\Models\Store;

use App\Models\BaseModel;
use DC\UserCenter\Models\UserBaseModel;
use Illuminate\Http\Request;

class ShopStoreModel extends BaseModel
{
    protected $table = 'shop_store';

    protected $primaryKey = 'store_id';

    public $dateFormat = 'U';

    protected $guarded = [];

    public function info()
    {
        return $this->hasOne('App\Models\Store\ShopStoreInfoModel','store_id','store_id');
    }

    public function user()
    {
        return $this->belongsTo('DC\UserCenter\Models\UserBaseModel','user_id','member_id');
    }

    public function addStore(Request $request)
    {
        $user = $request->post('user_number');
        if(!$user){
            $this->errorMsg = '请输入店铺所属会员';
            return false;
        }
        $user_id = UserBaseModel::where('user_number',$user)->value('member_id');

        if(!$user_id){
            $this->errorMsg = '请输入有效会员';
            return false;
        }
        if(self::where('user_id',$user_id)->first()){
            $this->errorMsg = '一个会员只能创建一个店铺';
            return false;
        }
        $store = new self();
        $store->store_name = $request->post('store_name');
        $store->store_title = $request->post('store_title');
        $store->store_description = $request->post('store_description');
        $store->keywords = $request->post('keywords');
        $store->store_logo = $request->post('store_logo');
        $store->category_ids = $request->post('category_ids');
        $store->store_type = $request->post('store_type');
        $store->status = $request->post('status',0);
        $store->is_recommend = $request->post('is_recommend');
        $store->user_id = $user_id;

        if($store->save()){
            $data['entity_fzrName'] = $data['company_name'] = $request->post('company_name');
            $data['store_type'] = $request->post('store_type');
            $data['ID_card'] = $data['b_license_card'] = $request->post('b_license_card');
            $data['ID_card_front'] = $data['b_license_front'] = $request->post('b_license_front');
            $data['ID_card_back'] = $data['b_license_back'] = $request->post('b_license_back');

            $data['phone'] = $data['entity_fzrMobile'] = $request->post('phone');
            $data['company_address'] = $request->post('company_address');
            $data['entity_comArea'] = $request->post('company_address');
            $data['entity_remark'] = $request->post('entity_remark');
            $data['entity_fzrEmail'] = $request->post('entity_fzrEmail');
            if($data['store_type'] == 1){
                $data['entity_fzrName'] = $request->post('entity_fzrName');//负责人姓名
                $data['entity_fzrMobile'] = $request->post('entity_fzrMobile'); // 负责人联系方式
                $data['ID_card'] = $request->post('ID_card');
                $data['ID_card_front'] = $request->post('ID_card_front');
                $data['ID_card_back'] = $request->post('ID_card_back');
            }

            $store->info()->save(new ShopStoreInfoModel($data));
            return true;
        }
        return false;

    }

    public function editStore(Request $request, $store_id)
    {
        $store = self::find($store_id);
        $store->store_name = $request->post('store_name');
        $store->store_title = $request->post('store_title');
        $store->store_description = $request->post('store_description');
        $store->keywords = $request->post('keywords');
        $store->store_logo = $request->post('store_logo');
        $store->category_ids = $request->post('category_ids');
        $store->store_type = $request->post('store_type');
        $store->status = $request->post('status',0);
        $store->is_recommend = $request->post('is_recommend');
        if($store->save()){
            return true;
        }
        return false;
    }

    public function deleteStore($store_id)
    {
        $info = self::find($store_id);
        if(!$info){
            $this->errorMsg = '要删除的资源不存在!';
            return false;
        }
        if(!$info->delete()){
            ShopStoreInfoModel::where('store_id',$store_id)->delete();
            $this->errorMsg = '删除失败!';
            return false;
        }
        return true;
    }
}
